/**
 * 应用的核心js文件
 */
var grApp = angular.module('gr-app', ['toaster', 'ngAnimate']);

//绑定了产品操作的控制器
grApp.controller('ProductCtrl',['$scope','$http',function($scope,$http){

}]);

grApp.controller('DetailCtrl',function($scope,$http){

    $scope.prices = [];
    $scope.init = function(pricejson,number){
        $scope.prices = pricejson;
        //TODO:解析出价格区间的规则
        $scope.price = parseFloat(pricejson[0].grprice);
        $scope.psid = pricejson[0].id;
        $scope.data.size = number;
    }


    /**
     * 获取价格的函数
     * @param size
     * @returns {demoData.spec.grprice|*|grprice}
     */
    function getPrice(size){
        var arr = $scope.prices;
        var len = arr.length;
        for(var i = len-1; i>=0 ; i--){
            if(size>=arr[i].point){
                return arr[i];
            }
        }
    }

    $scope.data = {size:1};
    $scope.psid = 1;

    $scope.plus = function(){
        $scope.data.size++;
        var price = getPrice($scope.data.size);
        $scope.price = parseFloat(price.grprice);
        $scope.psid = price.id;
    }

    $scope.sub = function(){
        if($scope.data.size == 1) return;
        $scope.data.size--;
        var price = getPrice($scope.data.size);
        $scope.price = parseFloat(price.grprice);
        $scope.psid = price.id;
    }

    $scope.test = function(){
        alert('test');
    }

    $scope.addToCart = function(pid){
        var param = {pid:pid,psid:$scope.psid,number:$scope.data.size,price:$scope.price};

        $http.post("/extra/diy/cart/add",param).success(function(res){
            alert('Yes!添加成功了!');
        }).error(function(err){
            console.log('Ops!抱歉\r\n果小然可能遇到什么棘手的事情了,待会再来试试吧～')
        });
    };
});

grApp.controller('CartCtrl',function($scope,$http){
    $scope.moveToTrash = function(pid){
        alert(pid);
    }
});
